package com.italicatab;

import it.sephiroth.android.library.widget.HListView;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;

public class Projects extends Activity implements OnClickListener {
	// All static variables
	static final String URL = "http://makemyonlineshop.com/demo/italica/api/v1/get_all_projects";
	ProgressBar PROGRESS_BAR;
	NetworkInfo nwinfo;
	HListView CAT_LIST;
	AQuery AQ;
	TextView TV_HEADER;
	ImageView IV_PRODUCTS, IV_ABOUT_US, IV_CONTACT_US, IV_HOME, IV_PROJECTS;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.projects);
		init();
		AQ = new AQuery(Projects.this);
		if (General.getAllProject != null && General.getAllProject.size() > 0) {
			CAT_LIST.setAdapter(new LazyAdapterGrid(Projects.this));
			// General.menuProducts = new ArrayList<HashMap<String, String>>();
			// new GetAllProducts().execute();
		} else {
			if (isOnline()) {
				General.getAllProject = new ArrayList<HashMap<String, String>>();
				new GetAllProducts().execute();
			} else {
				Toast.makeText(getApplicationContext(),
						"Please Check Internet connection", Toast.LENGTH_SHORT)
						.show();
			}

		}
	}

	private void init() {
		IV_PRODUCTS = (ImageView) findViewById(R.id.iv_product);
		IV_PRODUCTS.setOnClickListener(this);
		IV_ABOUT_US = (ImageView) findViewById(R.id.iv_about);
		IV_ABOUT_US.setOnClickListener(this);
		IV_CONTACT_US = (ImageView) findViewById(R.id.iv_contact);
		IV_CONTACT_US.setOnClickListener(this);
		IV_HOME = (ImageView) findViewById(R.id.iv_home);
		IV_HOME.setOnClickListener(this);
		IV_PROJECTS = (ImageView) findViewById(R.id.iv_projects);
		IV_PROJECTS.setOnClickListener(this);
		PROGRESS_BAR = (ProgressBar) findViewById(R.id.progressBar1);
		PROGRESS_BAR.setVisibility(View.GONE);
		CAT_LIST = (HListView) findViewById(R.id.hListView1);

		TV_HEADER = (TextView) findViewById(R.id.tv_header);

	}

	public class GetAllProducts extends AsyncTask<Void, Integer, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			PROGRESS_BAR.setVisibility(View.VISIBLE);
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			XMLParser parser = new XMLParser();
			String xml = parser.getXmlFromUrl(URL); //

			// XML
			// Log.e("magnum", "product data is"+xml);
			Document doc = parser.getDomElement(xml); // getting DOM element
			doc.getDocumentElement().normalize();

			NodeList nl = doc.getElementsByTagName("project");
			// looping through all item nodes <item>
			for (int i = 0; i < nl.getLength(); i++) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				Element e = (Element) nl.item(i);
				// adding each child node to HashMap key => value
				map.put("img", parser.getValue(e, "img"));
				map.put("projectname", parser.getValue(e, "projectname"));
				map.put("location", parser.getValue(e, "location"));
				map.put("city", parser.getValue(e, "city"));

				// adding HashList to ArrayList
				General.getAllProject.add(map);
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			PROGRESS_BAR.setVisibility(View.GONE);
			CAT_LIST.setAdapter(new LazyAdapterGrid(Projects.this));

		}
	}

	public class LazyAdapterGrid extends BaseAdapter {
		// it is a custom adapter by this we are showing driver login
		// information as a list
		private Activity activity;
		private String[] data;
		private LayoutInflater inflater = null;

		// public ImageLoader imageLoader;

		public LazyAdapterGrid(Activity a) {
			activity = a;
			// data = d; //its image path
			inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// imageLoader = new ImageLoader(activity.getApplicationContext());
		}

		public int getCount() {
			return 30;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// View vi = convertView;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.raw_project, parent,
						false);
			}

			ImageView iv = (ImageView) convertView
					.findViewById(R.id.iv_project_photo);

			clsLazyLoad lazy = new clsLazyLoad(Projects.this, true);
			String url = General.getAllProject.get(position).get("img");
			iv.setTag(url);
			lazy.DisplayImage(url, Projects.this, iv);
			TextView tv_name = (TextView) convertView
					.findViewById(R.id.tv_project_name);
			tv_name.setText(General.getAllProject.get(position).get(
					"projectname"));
			TextView tv_address = (TextView) convertView
					.findViewById(R.id.tv_project_address);
			tv_address.setText(General.getAllProject.get(position).get(
					"location")
					+ ", " + General.getAllProject.get(position).get("city"));

			// String cat =
			// General.menuProducts.get(position).get("productname");
			// tv_name.setText(cat);
			// String url = General.menuProducts.get(position).get("src");
			// System.out.println(url);
			// AQ.id(iv).image(url);

			return convertView;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private boolean isOnline() {

		// it provides Internet information
		ConnectivityManager conmanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		nwinfo = conmanager.getActiveNetworkInfo();
		return nwinfo != null;
	}

	@Override
	public void onClick(View v) {
		if (PROGRESS_BAR.getVisibility() == View.VISIBLE) {
			return;
		}
		if (v == IV_ABOUT_US) {
			Intent i = new Intent(getApplicationContext(), AboutUs.class);
			startActivity(i);
			finish();
		} else if (v == IV_CONTACT_US) {
			Intent i = new Intent(getApplicationContext(), ContactUs.class);
			startActivity(i);
			finish();
		} else if (v == IV_HOME) {
			Intent i = new Intent(getApplicationContext(), HomeScreen.class);
			startActivity(i);
			finish();
		} else if (v == IV_PRODUCTS) {
			Intent i = new Intent(getApplicationContext(), Product.class);
			startActivity(i);
			finish();
		}
	}

}
